#include "UThemedButton.h"
#include "UControl.h"
#include "../UTextureManager.h"
#include "../UControlContainerWindow.h"


using namespace std;

TThemedButton::TThemedButton(std::string textures[6], float posinfo[], int rw, int rh)
: TButton(TRectangle())
, rheight(rh), rwidth(rw)
{
    for (int i = 0; i < 6; ++i)
        texture[i] = GTextureManager->getTexture(textures[i], true, GL_LINEAR, GL_LINEAR, true);
        
  glBindTexture(GL_TEXTURE_2D, texture[0]);
  GLfloat w, h;
	glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
	glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
	width = w;
	height = h;

	posrelx = posinfo[0];
	posabsx = posinfo[1];
	posrely = posinfo[2];
	posabsy = posinfo[3];
}

void TThemedButton::Draw()
{
	glColor3f(1,1,1);
	glEnable(GL_TEXTURE_2D);
	int texnum = 0;
	if (Pressed)
        texnum = 2;
  else if (MouseOver)
      texnum = 1;
  
  //if (Parent->IsFocussed())
  //    texnum += 3;
  glBindTexture(GL_TEXTURE_2D, texture[texnum]);
  glEnable(GL_BLEND);
  glBegin(GL_QUADS);
      glTexCoord2i(0,0); glVertex2i(Left	     , Top);
      glTexCoord2i(1,0); glVertex2i(Left+width , Top);
      glTexCoord2i(1,1); glVertex2i(Left+width , Top+height); // hax
      glTexCoord2i(0,1); glVertex2i(Left	     , Top+height); // hax
  glEnd();
  glDisable(GL_TEXTURE_2D);
  glDisable(GL_BLEND);
    
    
}

void TThemedButton::setPosRelative(TRectangle rect)
{
	int windowWidth = rect.Right - rect.Left;
	int windowHeight = rect.Bottom - rect.Top;
	
	int left = ((windowWidth-rwidth)*posrelx) + posabsx;
	int top = ((windowHeight-rheight)*posrely) + posabsy;
	
	setPos(TRectangle(left, top, left+rwidth, top+rheight));
}


